package com.eax.CLILayer;
import com.eax.ControlLayer.*;
import com.eax.ModelLayer.*;

import java.text.DateFormat;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Scanner;

public class ReservationUI {
	private ReservationController reservationController;
	private DVDController dvdController;
	private AddressController addressController;

	public ReservationUI(ReservationController reservationController, DVDController dvdController, AddressController addressController) {
		this.reservationController = reservationController;
		this.dvdController = dvdController;
		this.addressController = addressController;
	}

	public void ReservationMenu() {
		boolean exit = false;

		while(!exit) {
			//show menu and return the user's choice
			int choice = writeReservationMenu();

			switch (choice) {
				case 1:
					findReservation();
					break;
				case 2:
					createReservation();
					break;
				case 3:
					deleteReservation();
					break;
				case 4:
					listAllReservations();
					break;
				default:
					exit = true;
					break;
			}
		}
	}

	private int writeReservationMenu()  {
		Scanner keyboard = new Scanner(System.in);
		System.out.println("\f *** Reservation Menu ***");
		System.out.println(" (1) Find a reservation");
		System.out.println(" (2) Create a reservation");
		System.out.println(" (3) Delete a reservation");
		System.out.println(" (4) Show all reservations");
		System.out.println(" (6) Return");
		System.out.print("\n Make your choice: ");

		return keyboard.nextInt();
	}

	private void findReservation() {
		int id = inputId();
		ReservationModel x = reservationController.getReservationById(id);
		x.printInfo();
	}

	private void createReservation(){
		int id = reservationController.getHighestId() + 1;
		int personID = inputPerson();
		int DVDID = inputGetDvd();
		String status = inputStatus();
		String reservationDate = inputReservationDate();

		reservationController.addReservation(id, personID, DVDID, status, reservationDate);
	}

	private void deleteReservation(){
		int id = inputId();
		ReservationModel reservation = reservationController.getReservationById(id);
		reservationController.removeReservation(id);
	}

	private void listAllReservations(){
		for(ReservationModel r : reservationController.getAllReservations()){
			r.printInfo();
		}
	}

	private int inputId(){
		Scanner keyboard = new Scanner(System.in);
		System.out.print("Type id: ");
		return keyboard.nextInt();
	}

	private int inputPerson(){
		Boolean ok = false;
		PersonModel person = null;
		String input;
		int id = 0;

		while(!ok){
			Scanner keyboard = new Scanner(System.in);
			System.out.print("Find person - Type search input: ");
			input = keyboard.nextLine();

		 	person = addressController.findPerson(input);

			if(person != null){
				id = person.getId();
				ok = true;
			} else {
				System.out.println("Person not found. Try again!");
			}
		}

		return id;
	}

	private int inputGetDvd(){
		int copySerialNumber = 0;
		int choice;
		Boolean ok = false;
		ArrayList<DVDModel> dvds = dvdController.getAllDvds();

		while(!ok){
			System.out.println("Please select a DVD to create a reservation for: ");
			for(DVDModel d : dvds){
				System.out.println(d.getId() + ") "+d.getTitle());
			}

			Scanner keyboard = new Scanner(System.in);
			System.out.print("Selection: ");
			choice = keyboard.nextInt();

			DVDModel dvd = dvdController.findDVD(choice);
			if(dvd != null){
				ok = true;
				copySerialNumber = choice;
			} else {
				System.out.println("DVD Not found. Please try again.");
			}
		}

		return copySerialNumber;
	}

	private String inputStatus(){
		Scanner keyboard = new Scanner(System.in);
		System.out.print("Type the status of the reservation: ");
		return keyboard.nextLine();
	}

	private String inputReservationDate(){
		Scanner keyboard = new Scanner(System.in);
		System.out.print("Type the reservation date (0 = today): ");
		String input = keyboard.nextLine();

		if(input.equals("0")){
			DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
			Date date = new Date();
			input =  dateFormat.format(date);
		}

		return input;
	}
}